package com.wanxi.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NewsModel;
import com.wanxi.service.Impl.NewsServiceImpl;
import com.wanxi.service.NewsService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/7 11:57
 */
@WebServlet("/back/FindNewsById")
public class FindNewsByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //接收前端数据
        String id = req.getParameter("id");
        //封装model
        NewsModel newsModel = NewsModel.builder()
                .id(Tool.nullToInt(id))
                .build();
        //调用service层
        NewsService newsService = new NewsServiceImpl();
        ResultDto resultDto = newsService.findById(newsModel);
        //将数据返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDto));
    }
}
